#include "src\precompiled.h"
#include "src\string_converter.h"

std::string string_converter::to_string( INT8 val )
{
	std::stringstream s;
	s << val;
	return s.str();
}

std::string string_converter::to_string( UINT8 val )
{
	std::stringstream s;
	s << val;
	return s.str();
}
std::string string_converter::to_string( INT16 val )
{
	std::stringstream s;
	s << val;
	return s.str();
}

std::string string_converter::to_string( UINT16 val )
{
	std::stringstream s;
	s << val;
	return s.str();
}

std::string string_converter::to_string( INT32 val )
{
	std::stringstream s;
	s << val;
	return s.str();
}

std::string string_converter::to_string( UINT32 val )
{
	std::stringstream s;
	s << val;
	return s.str();
}

std::string string_converter::to_string( bool val )
{
	std::stringstream s;
	s << (val ? "true" : "false");
	return s.str();
}

std::string string_converter::to_string( float val )
{
	std::stringstream s;
	s.setf(std::ios::fixed);
	s.precision(4);
	s << val;
	return s.str();
}

INT8 string_converter::get_s8( const std::string &str, INT8 def )
{
	std::stringstream s;
	s.str(str);
	INT8 val = def;
	s >> val;
	return val;
}

UINT8 string_converter::get_u8( const std::string &str, UINT8 def )
{
	std::stringstream s;
	s.str(str);
	UINT8 val = def;
	s >> val;
	return val;
}

INT16 string_converter::get_s16( const std::string &str, INT16 def )
{
	std::stringstream s;
	s.str(str);
	INT16 val = def;
	s >> val;
	return val;
}

UINT16 string_converter::get_u16( const std::string &str, UINT16 def )
{
	std::stringstream s;
	s.str(str);
	UINT16 val = def;
	s >> val;
	return val;
}

INT32 string_converter::get_s32( const std::string &str, INT32 def )
{
	std::stringstream s;
	s.str(str);
	INT32 val = def;
	s >> val;
	return val;
}

UINT32 string_converter::get_u32( const std::string &str, UINT32 def )
{
	std::stringstream s;
	s.str(str);
	UINT32 val = def;
	s >> val;
	return val;
}

bool string_converter::get_bool( const std::string &str, bool def )
{
	std::string boolean = to_lower_case(str);
	if( boolean == std::string("true") )
		return true;
	return def;
}

float string_converter::get_float( const std::string &str, float def )
{
	std::stringstream s;
	s.str(str);
	float val = def;
	s >> val;
	return val;
}

std::string string_converter::to_lower_case( const std::string &str )
{
	std::string result = str;
	std::transform( result.begin(), result.end(), result.begin(), tolower );
	return result;
}

std::string string_converter::to_upper_case( const std::string &str )
{
	std::string result = str;
	std::transform( result.begin(), result.end(), result.begin(), toupper );
	return result;
}

std::string string_converter::replace(const std::string &source, const std::string &find, const std::string &paste)
{
	if(find == paste)
		return source;
	size_t offset = 0;
	std::string result = source;
	do {
		offset = result.find(find, offset);
		if(offset != std::string::npos)
			result.replace(offset, paste.length(), paste);
	}
	while(offset != std::string::npos);
	return result;
}